/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jjsoft.celularessuc.gco.ui;


import com.jjsoft.celularessuc.gco.mdl.Sucursal;
import com.jjsoft.celularessuc.gco.neg.SucursalManager;
import com.jjsoft.celularessuc.gco.util.MiModelo;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;

/**
 *
 * @author Johnny
 */
public class VMantenimientoSucursales extends javax.swing.JFrame {

    /**
     * Creates new form VMantenimientoSucursals
     */
    SucursalManager cm = new SucursalManager();
    List<Sucursal> listaSucursals = new ArrayList<Sucursal>();
    private static VMantenimientoSucursales instancia;

    public VMantenimientoSucursales() {
        initComponents();
        listaSucursals = cm.listarSucursals();
        cargarDatos();
        this.cmdTodo.setVisible(false);
    }

    public static VMantenimientoSucursales getInstance() {
        if(instancia==null)
            instancia= new VMantenimientoSucursales();
        return instancia;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mnuAcciones = new javax.swing.JPopupMenu();
        mnuModificar = new javax.swing.JMenuItem();
        mnuEliminar = new javax.swing.JMenuItem();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        dtgSucursales = new javax.swing.JTable();
        cmdEliminar = new javax.swing.JButton();
        cmdModificar = new javax.swing.JButton();
        cmdNuevo = new javax.swing.JButton();
        cmdTodo = new javax.swing.JButton();

        mnuModificar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/edit21.png"))); // NOI18N
        mnuModificar.setText("Modificar");
        mnuModificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuModificarActionPerformed(evt);
            }
        });
        mnuAcciones.add(mnuModificar);

        mnuEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/delete.png"))); // NOI18N
        mnuEliminar.setText("Eliminar");
        mnuEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuEliminarActionPerformed(evt);
            }
        });
        mnuAcciones.add(mnuEliminar);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel2.setFont(new java.awt.Font("Ubuntu", 1, 18)); // NOI18N
        jLabel2.setText("Mantenimiento de Sucursales");

        dtgSucursales.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        dtgSucursales.setComponentPopupMenu(mnuAcciones);
        dtgSucursales.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(dtgSucursales);

        cmdEliminar.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        cmdEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/delete.png"))); // NOI18N
        cmdEliminar.setText("Eliminar");
        cmdEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdEliminarActionPerformed(evt);
            }
        });

        cmdModificar.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        cmdModificar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/edit.png"))); // NOI18N
        cmdModificar.setText("Modificar");
        cmdModificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdModificarActionPerformed(evt);
            }
        });

        cmdNuevo.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        cmdNuevo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/new.png"))); // NOI18N
        cmdNuevo.setText("Nuevo");
        cmdNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdNuevoActionPerformed(evt);
            }
        });

        cmdTodo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdTodoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(63, 63, 63)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(cmdTodo, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(23, 23, 23)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(cmdEliminar)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cmdModificar)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cmdNuevo)))))
                .addContainerGap(27, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(11, 11, 11))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(25, 25, 25)
                        .addComponent(cmdTodo)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 199, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmdNuevo)
                    .addComponent(cmdEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cmdModificar, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        setSize(new java.awt.Dimension(405, 359));
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void cmdEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdEliminarActionPerformed
        // TODO add your handling code here:
        if (dtgSucursales.getSelectedRow() == -1) {
            JOptionPane.showMessageDialog(null, "Primero seleccione una sucursal", "Error", JOptionPane.ERROR_MESSAGE);
        } else {
            int i = dtgSucursales.getSelectedRow();
            if (JOptionPane.showConfirmDialog(rootPane, "Esta seguro de eliminar esta sucursal?", "Eliminar", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION) {
                cm.eliminarSucursal(listaSucursals.get(i));
                listaSucursals.remove(i);
                cargarDatos();
            }
        }
    }//GEN-LAST:event_cmdEliminarActionPerformed

    private void cmdModificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdModificarActionPerformed
        // TODO add your handling code here:
        if (dtgSucursales.getSelectedRow() == -1) {
            JOptionPane.showMessageDialog(null, "Primero seleccione un Producto", "Error", JOptionPane.ERROR_MESSAGE);
        } else {

            new VNuevaSucursal(listaSucursals.get(dtgSucursales.getSelectedRow()), cmdTodo).show();

        }
    }//GEN-LAST:event_cmdModificarActionPerformed

    private void cmdNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdNuevoActionPerformed
        // TODO add your handling code here:
        new VNuevaSucursal(cmdTodo).setVisible(true);
    }//GEN-LAST:event_cmdNuevoActionPerformed

    private void cmdTodoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdTodoActionPerformed
        // TODO add your handling code here:
        listaSucursals = cm.listarSucursals();
        cargarDatos();
    }//GEN-LAST:event_cmdTodoActionPerformed

    private void mnuModificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuModificarActionPerformed
        // TODO add your handling code here:
        cmdModificar.doClick();
    }//GEN-LAST:event_mnuModificarActionPerformed

    private void mnuEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuEliminarActionPerformed
        // TODO add your handling code here:
        cmdEliminar.doClick();
    }//GEN-LAST:event_mnuEliminarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(VMantenimientoSucursales.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(VMantenimientoSucursales.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(VMantenimientoSucursales.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(VMantenimientoSucursales.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new VMantenimientoSucursales().setVisible(true);
            }
        });
    }

    private void cargarDatos() {
        MiModelo modelo = new MiModelo();
        String[] identificadores = new String[]{"Id", "Nombre"};
        modelo.setColumnIdentifiers(identificadores);
        for (int i = 0; i < listaSucursals.size(); i++) {
            String[] unidad = new String[]{listaSucursals.get(i).getIdsucursal().toString(), listaSucursals.get(i).getNombre()};
            modelo.addRow(unidad);

        }
        dtgSucursales.setModel(modelo);
        dtgSucursales.getColumn("Id").setPreferredWidth(25);

    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cmdEliminar;
    private javax.swing.JButton cmdModificar;
    private javax.swing.JButton cmdNuevo;
    private javax.swing.JButton cmdTodo;
    private javax.swing.JTable dtgSucursales;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPopupMenu mnuAcciones;
    private javax.swing.JMenuItem mnuEliminar;
    private javax.swing.JMenuItem mnuModificar;
    // End of variables declaration//GEN-END:variables
}
